RM.Pages.Functions.Groups_Unit = function(unit){
    if (unit.unit.group_id == null) {
        return;
    }

    Ext.getCmp('edit_unit_group_id').setValue(RM.Translate.Common.LoadingMessage);
    Ext.getCmp('edit_unit_group_id').disable();
    Ext.Ajax.request({
        url: RM.Common.AssembleURL({
            controller: 'Groups_Edit',
            action: 'editjson',
            parameters: [{
                name: 'id',
                value: unit.unit.group_id
            },{
                name: 'iso',
                value: RM.Pages.Units_Edit_Language_Combo.getValue()
            }]
        }),
        method: 'POST',
        success: function(responseObject) {
            var group = Ext.util.JSON.decode(responseObject.responseText);
            RM.Pages.Groups_Edit_Panel_Units_Json.reload({
                callback : function(){
                    Ext.getCmp('edit_unit_group_id').setValue(group.id);
                    Ext.getCmp('edit_unit_group_id').setRawValue(group.name);
                    Ext.getCmp('edit_unit_group_id').enable();
                }
            });                                    
        }
    });
}

RM.Pages.Functions.Groups_Unit_Save = function(before){
    try{
        // this is in a try so that if groups are not enabled for the unit and
        // the edit_unit_group_id is not valid it will not through any errors
        if (before) {
            if (!Ext.getCmp('edit_unit_group_id').getValue()) {
                Ext.getCmp('edit_unit_group_id').disable();
            }
        } else {
            Ext.getCmp('edit_unit_group_id').enable();
        }
    } catch (e){}
}

RM.Pages.Units_EditJson_Handlers.push(RM.Pages.Functions.Groups_Unit);
RM.Pages.Units_EditJson_SaveHandlers.push(RM.Pages.Functions.Groups_Unit_Save);